package util
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import spark.components.Image;

	
	
	public class myCanvasImage extends Image
	{
		private var imageLoader:Loader = new Loader();
		
		public function set src(ba:ByteArray):void{
			
			//return;
			
			if(ba == null) {
				return;
			}
			
			var loader:Loader = new Loader();
			loader.loadBytes(ba);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);

			//loadImage(s);
		//	image_completeHandler2(bd);
		}
		
		private function loaderCompleteHandler(event:Event):void
		{
			var loader:Loader = (event.target as LoaderInfo).loader;
			var bmp:Bitmap = Bitmap(loader.content);
			image_completeHandler2(bmp);
			//image1.source = bmp;
		}
		
		public function myCanvasImage(){
		}
		
		private function loadImage(src:String):void {
			var request:URLRequest = new URLRequest(src);
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, image_completeHandler);
			imageLoader.load(request);
		}
		
		private function image_completeHandler2(bmp:Bitmap):void{
			var bmd:BitmapData = Bitmap(bmp).bitmapData;
			
			var originalWidth:uint = bmd.width;
			var originalHeight:uint = bmd.height;
			var newWidth:uint = width;
			var newHeight:uint = height;			
			if(originalWidth > newWidth || originalHeight > newHeight)
			{
				var m:Matrix = new Matrix();
				var scaleX:Number = 1;
				var scaleY:Number = 1;
				var scale:Number = 1;
				var sx:Number = newWidth / originalWidth;
				var sy:Number = newHeight / originalHeight;
				scale = Math.min(sx, sy);
				newWidth = originalWidth * scale;
				newHeight = originalHeight * scale;
				m.scale(scale, scale);
				var newbmd:BitmapData = new BitmapData(newWidth, newHeight);
				newbmd.draw(bmd, m);
				source = new Bitmap(newbmd);
			}
			else
			{
				source = new Bitmap(bmd);
			}
			
			imageLoader.unload();
			bmd.dispose();
			bmd = null;
		}
		
		private function image_completeHandler(event:Event):void{
			var bmd:BitmapData = Bitmap(event.currentTarget.content).bitmapData;
			
			var originalWidth:uint = bmd.width;
			var originalHeight:uint = bmd.height;
			var newWidth:uint = width;
			var newHeight:uint = height;			
			if(originalWidth > newWidth || originalHeight > newHeight)
			{
				var m:Matrix = new Matrix();
				var scaleX:Number = 1;
				var scaleY:Number = 1;
				var scale:Number = 1;
				var sx:Number = newWidth / originalWidth;
				var sy:Number = newHeight / originalHeight;
				scale = Math.min(sx, sy);
				newWidth = originalWidth * scale;
				newHeight = originalHeight * scale;
				m.scale(scale, scale);
				var newbmd:BitmapData = new BitmapData(newWidth, newHeight);
				newbmd.draw(bmd, m);
				source = new Bitmap(newbmd);
			}
			else
			{
				source = new Bitmap(bmd);
			}
			
			imageLoader.unload();
		}
	}
}